package com.SouthernWall_404.AgeDust.Client.Gui.Screen;

import com.SouthernWall_404.AgeDust.Common.API.Info.AttributesInfo;
import com.SouthernWall_404.AgeDust.Common.AgeDust;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import com.SouthernWall_404.AgeDust.Common.API.Enum.sexEnum;
import com.SouthernWall_404.AgeDust.Common.API.Enum.raceEnum;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Quaternionfc;

import javax.annotation.Nullable;

public class CharacterScreen extends Screen {

    private static final GuiGraphics GUIGRAPHICS=null;
    private float xMouse;
    private float yMouse;

    private static final String GUI="gui."+AgeDust.MODID+".";

    public int GAP=10;

    public String name="null";
    public String name_toChange="";

    public String age="18";
    public String age_toChange="";

    public sexEnum sex=sexEnum.male;
    public raceEnum race=raceEnum.human;

    public Button nameButton= Button.builder(Component.translatable(GUI+"character_screen.button.name"),b->{}).build();
    public Button sexButton= Button.builder(Component.translatable(GUI+"character_screen.button.sex"),b->{}).build();
    public Button raceButton= Button.builder(Component.translatable(GUI+"character_screen.button.race"),b->{}).build();
    public Button ageButton= Button.builder(Component.translatable(GUI+"character_screen.button.age"),b->{}).build();
    public Button comfirmButton= Button.builder(Component.translatable(GUI+"button.comfirm"),b->save()).build();;

    public EditBox nameEditBox;
    public Button sexEditBox=Button.builder(Component.translatable(""),b->onSexClick()).build();
    public Button raceEditBox=Button.builder(Component.translatable(""),b->onRaceClick()).build();
    public EditBox ageEditBox;

    public AttributesInfo attributesInfo = new AttributesInfo();

    public CharacterScreen(Component p_96550_, String name, sexEnum sex1, raceEnum race1, int age, AttributesInfo attributesInfo) {
        super(p_96550_);

        this.attributesInfo = attributesInfo;
        this.name = name;
        sex = sex1;
        race = race1;
        this.age = "" + age;

    }

    @Override
    public void render(GuiGraphics guiGraphics, int n1, int n2, float f) {
        this.renderBackground(guiGraphics);
        this.addPlayer(guiGraphics);
        this.xMouse = n1;
        this.yMouse = n2;
        super.render(guiGraphics, n1, n2, f);

    }

    @Override
    public void renderBackground(GuiGraphics guiGraphics) {
        super.renderBackground(guiGraphics);

    }

    @Override
    protected void init() {

//        this.name= Minecraft.getInstance().player.getDisplayName().getString();
        this.name_toChange=this.name;
        this.age_toChange=this.age;



        this.addWeights();
        super.init();



    }
    private void addWeights()
    {
        //this.addComfirmButton();

        addName();
        addSex();
        addAge();
        addRace();

        addComfirmButton();



    }


    private void addPlayer(GuiGraphics guiGraphics)
    {
        int n3 = this.width/5*3+GAP;
        int n4 = this.height/2-30;
        this.renderEntityInInventoryFollowsMouse(guiGraphics, n3 + 51, n4 + 75, 60, (float)(n3 + 51) -this.xMouse, (float)(n4 + 75 - 50) - this.yMouse, this.minecraft.player);
    }

    public void renderEntityInInventoryFollowsMouse(GuiGraphics guiGraphics, int n, int n2, int n3, float f, float f2, LivingEntity livingEntity) {
        float f3 = (float)Math.atan(f / 40.0f);
        float f4 = (float)Math.atan(f2 / 40.0f);
        Quaternionf quaternionf = new Quaternionf().rotateZ((float)Math.PI);
        Quaternionf quaternionf2 = new Quaternionf().rotateX(f4 * 20.0f * ((float)Math.PI / 180));
        quaternionf.mul((Quaternionfc)quaternionf2);
        float f5 = livingEntity.yBodyRot;
        float f6 = livingEntity.getYRot();
        float f7 = livingEntity.getXRot();
        float f8 = livingEntity.yHeadRotO;
        float f9 = livingEntity.yHeadRot;
        livingEntity.yBodyRot = 180.0f + f3 * 20.0f;
        livingEntity.setYRot(180.0f + f3 * 40.0f);
        livingEntity.setXRot(-f4 * 20.0f);
        livingEntity.yHeadRot = livingEntity.getYRot();
        livingEntity.yHeadRotO = livingEntity.getYRot();
        this.renderEntityInInventory(guiGraphics, n, n2, n3, quaternionf, quaternionf2, livingEntity);
        livingEntity.yBodyRot = f5;
        livingEntity.setYRot(f6);
        livingEntity.setXRot(f7);
        livingEntity.yHeadRotO = f8;
        livingEntity.yHeadRot = f9;
    }

    public void renderEntityInInventory(GuiGraphics guiGraphics, int n, int n2, int n3, Quaternionf quaternionf, @Nullable Quaternionf quaternionf2, LivingEntity livingEntity) {
        guiGraphics.pose().pushPose();//压栈
        guiGraphics.pose().translate((double)n, (double)n2, 50.0);//平移
        guiGraphics.pose().mulPoseMatrix(new Matrix4f().scaling((float)n3, (float)n3, (float)(-n3)));//缩放
        guiGraphics.pose().mulPose(quaternionf);//旋转
        Lighting.setupForEntityInInventory();
        EntityRenderDispatcher entityRenderDispatcher = Minecraft.getInstance().getEntityRenderDispatcher();
        if (quaternionf2 != null) {
            quaternionf2.conjugate();
            entityRenderDispatcher.overrideCameraOrientation(quaternionf2);
        }
        entityRenderDispatcher.setRenderShadow(false);
        RenderSystem.runAsFancy(() -> entityRenderDispatcher.render(livingEntity, 0.0, 0.0, 0.0, 0.0f, 1.0f, guiGraphics.pose(), guiGraphics.bufferSource(), 0xF000F0));
        guiGraphics.flush();
        entityRenderDispatcher.setRenderShadow(true);
        guiGraphics.pose().popPose();
        Lighting.setupFor3DItems();
    }
    /*
    添加姓名栏
     */
    private void addName()
    {
        addNameButton();
        addNameBox();


    }

    private void addNameButton()
    {
        int l=40;
        int h=20;
        int n1 = this.width/5;
        int n2 = this.height/2-h*2-GAP*3/2;

        nameButton.setHeight(h);
        nameButton.setWidth(l);
        nameButton.setPosition(n1,n2);

        addRenderableWidget(nameButton);
    }
    private void addNameBox()
    {

        int l=120;
        int h=20;
        int n3 = this.width /5+40+GAP;
        int n4 = this.height/2-h*2-GAP*3/2;
        this.nameEditBox = new EditBox(
                this.font,
                n3,
                n4,
                l,
                h,
                Component.translatable("container.repair"));
        //this.nameEditBox.render(guiGraphics,n1,n2,f);
        //this.nameEditBox.setResponder(this::onStringChanged);

        this.nameEditBox.setCanLoseFocus(false);
        this.nameEditBox.setTextColor(-1);
        this.nameEditBox.setTextColorUneditable(-1);
        this.nameEditBox.setBordered(true);
        this.nameEditBox.setMaxLength(50);
        this.nameEditBox.setValue(this.name);
        this.nameEditBox.setResponder(this::onStringChanged);
       // this.addWidget(this.nameEditBox);
        //this.setInitialFocus(this.nameEditBox);
        this.nameEditBox.setEditable(true);
        this.nameEditBox.setFocused(true);

        this.setNameActive();

        this.addRenderableWidget(this.nameEditBox);
    }
    public void setNameActive()
    {
        this.nameEditBox.active=false;
    }
    private void onStringChanged(String string)
    {
        this.name_toChange=string;
    }

    @Override
    public void tick() {
        super.tick();
        //this.nameEditBox.tick();
    }
    /*
 添加性别栏
  */
    public void addSex()
    {
        addSexButton();
        addSexAddButton();
    }
    //添加性别的标签栏
    private void addSexButton()
    {
        int l=40;
        int h=20;
        int n1 = this.width/5;
        int n2 = this.height/2-h-GAP*1/2;

        sexButton.setHeight(h);
        sexButton.setWidth(l);
        sexButton.setPosition(n1,n2);

        addRenderableWidget(sexButton);
    }
    //添加性别的编辑栏
    private void addSexAddButton()
    {
        int l=120;
        int h=20;
        int n1 = this.width/5+40+GAP;
        int n2 = this.height/2-h-GAP*1/2;

        sexEditBox.setHeight(h);
        sexEditBox.setWidth(l);
        sexEditBox.setPosition(n1,n2);
        sexEditBox.setMessage(Component.translatable(GUI+"character_screen.button.sex."+sex.name()));

        addRenderableWidget(sexEditBox);
    }
    //按下性别编辑栏时的反应。此处为空，应为sex改变
    public void onSexClick()
    {

    }


    /*
 添加种族栏
  */
    public void addRace()
    {
        addRaceButton();
        addRaceAddButton();



    }

    private void addRaceButton()
    {
        int l=40;
        int h=20;
        int n1 = this.width/5;
        int n2 = this.height/2+GAP*1/2;

        raceButton.setHeight(h);
        raceButton.setWidth(l);
        raceButton.setPosition(n1,n2);

        addRenderableWidget(raceButton);
    }
    //添加性别的编辑栏
    private void addRaceAddButton()
    {
        int l=120;
        int h=20;
        int n1 = this.width/5+40+GAP;
        int n2 = this.height/2+GAP*1/2;

        raceEditBox.setHeight(h);
        raceEditBox.setWidth(l);
        raceEditBox.setPosition(n1,n2);
        raceEditBox.setMessage(Component.translatable(GUI+"character_screen.button.race."+race.name()));

        addRenderableWidget(raceEditBox);
    }

    //按下性别编辑栏时的反应。此处为空，应为sex改变
    public void onRaceClick()
    {

    }
    /*
添加年龄栏
*/
    private void addAge()
    {
        addAgeButton();
        addAgeBox();



    }

    private void addAgeButton()
    {
        int l=40;
        int h=20;
        int n1 = this.width/5;
        int n2 = this.height/2+h+GAP*3/2;

        ageButton.setHeight(h);
        ageButton.setWidth(l);
        ageButton.setPosition(n1,n2);

        addRenderableWidget(ageButton);
    }
    private void addAgeBox()
    {

        int l=120;
        int h=20;
        int n3 = this.width /5+40+GAP;
        int n4 = this.height/2+h+GAP*3/2;
        this.ageEditBox = new EditBox(
                this.font,
                n3,
                n4,
                l,
                h,
                Component.translatable("container.repair"));
        //this.nameEditBox.render(guiGraphics,n1,n2,f);
        //this.nameEditBox.setResponder(this::onStringChanged);

        this.ageEditBox.setCanLoseFocus(false);
        this.ageEditBox.setTextColor(-1);
        this.ageEditBox.setTextColorUneditable(-1);
        this.ageEditBox.setBordered(true);
        this.ageEditBox.setMaxLength(50);
        this.ageEditBox.setValue(this.age);
        this.ageEditBox.setResponder(this::onAgeStringChanged);
        // this.addWidget(this.nameEditBox);
        //this.setInitialFocus(this.nameEditBox);
        this.ageEditBox.setEditable(true);
        this.ageEditBox.setFocused(true);



        this.addRenderableWidget(this.ageEditBox);
    }
    public void setAgeActive()
    {
        this.ageEditBox.active=false;
    }
    public void onAgeStringChanged(String string)
    {

    }




    public void addComfirmButton()
    {

    }
    public void save()
    {

    }
//
//    private void save()
//    {
//
//    }

}
